[assembly: WebActivator.PreApplicationStartMethod(typeof(MsForgeReboot.Web.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(MsForgeReboot.Web.App_Start.NinjectWebCommon), "Stop")]

namespace MsForgeReboot.Web.App_Start
{
    using System;
    using System.Data.Entity;
    using System.Web;
    using System.Web.Hosting;
    using System.Web.Http;
    using System.Web.Mvc;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using MsForgeReboot.Framework.Interfaces;
    using MsForgeReboot.Framework.ModelBinders;
    using MsForgeReboot.Framework.ViewModels;
    using MsForgeReboot.Services;
    using MsForgeReboot.Web.Modules;

    using Ninject;
    using Ninject.Web.Common;

    public static class NinjectWebCommon 
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));

            bootstrapper.Initialize(CreateKernel);

        }
        
        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }
        
        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

            RegisterServices(kernel);

            ModelBinders.Binders.Add(typeof(ImageViewModel), kernel.Get<ImageViewModelModelBinder>());

            return kernel;
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel) {

            kernel.Bind<MsForgeContext>().To<MsForgeContext>().InRequestScope();
            kernel.Bind<IBlogService>().To<BlogService>().InRequestScope();
            kernel.Bind<ICmsService>().To<CmsService>().InRequestScope();
            kernel.Bind<ISpeakerService>().To<SpeakerService>().InRequestScope(); 
            kernel.Bind<IUserService>().To<UserService>().InRequestScope();
            kernel.Bind<IUserGroupService>().To<UserGroupService>().InRequestScope();
            kernel.Bind<IEventService>().To<EventService>().InRequestScope();

            var mediaGalleryFolder = HostingEnvironment.MapPath(Properties.Settings.Default.MediaGalleryFolder);
            kernel.Bind<IMediaGalleryService>().To<MediaGalleryService>().InRequestScope().WithConstructorArgument("mediaGalleryFolder", mediaGalleryFolder);

            kernel.Bind<ICarouselService>().To<CarouselService>().InRequestScope();

            kernel.Bind<IHttpModule>().To<MessageDeliveryModule>().InSingletonScope();

        }        
    }
}
